package utilities.utilities_components;

import org.zkoss.zul.Button;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.Hbox;
import org.zkoss.zul.Label;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;

import java.util.Date;
import java.util.Calendar;
import java.util.List;

import utilities.Updatable;

public class Duration extends Window{
	private Label type;
	private Label to;
	private Combobox dateType;
	private Datebox begin;
	private Datebox end;
	private Calendar calendar;
	
	private Hbox hbox;
	private int currentSelected;
	
	private String[] metaInfo;
	
	/*  Constant  */
	private static final Date today = new Date();
	private static final String[] typeItem = { "自定", "今天", "昨天", "本周", "上周", "本月", "上月" };
	private static final int SELF_DEF = 0;
	private static final int TODAY = 1;
	private static final int YESTERDAY = 2;
	private static final int THIS_WEEK = 3;
	private static final int LAST_WEEK = 4;
	private static final int THIS_MON = 5;
	private static final int LAST_MON = 6;
	
	private static final String STD_WIDTH = "45pt"; 
	
	public Duration( String label ){
		super();
		
		this.metaInfo = metaInfo;
		
		hbox = new Hbox();
		
		type = new Label( label );
		to = new Label( "至" );
		
		this.calendar = Calendar.getInstance();
		
		this.prepareDateType();
		this.prepareBeginBox();
		this.prepareEndBox();
		
		hbox.appendChild( type );
		hbox.appendChild( dateType );
		hbox.appendChild( begin );
		hbox.appendChild( to );
		hbox.appendChild( end );
		
		this.appendChild( hbox );
	}
	
	/*  Initialize date type combobox  */
	private void prepareDateType(){
		dateType = new Combobox();                                          /*  Constructor  */
		
		dateType.setWidth( STD_WIDTH );
		
		for( int count = 0; count < typeItem.length; count++ )
			dateType.appendItem( typeItem[ count ] );                       /*  Fledged combobox  */
		
		dateType.setSelectedIndex( TODAY );
		currentSelected = TODAY;
	}
	
	private void prepareBeginBox(){
		begin = new Datebox();
		begin.setValue( today );
		
		begin.setDisabled( true );
	}
	
	private void prepareEndBox(){
		end = new Datebox();
		end.setValue( today );
		
		end.setDisabled( true );
	}
	
	private void disableDateBox( boolean disabled ){
		begin.setDisabled( disabled );
		end.setDisabled( disabled );
	}
	
	private boolean checkDateValidation(){
		Date beginDate = begin.getValue();
		Date endDate = end.getValue();
		
		if( beginDate.after( endDate ) ){
			try{
				new Messagebox().show( "起始时间晚于结束时间!", "错误", Messagebox.OK, null );
			}catch( InterruptedException e ){
				
			}
			return false;
		}else if( beginDate.after( today ) || endDate.after( today ) ){
			try{
				new Messagebox().show( "未定义的将来数据", "错误", Messagebox.OK, null );
			}catch( InterruptedException e ){
				
			}
			return false;
		}else{
			return true;
		}
	}
	
	public Date getBegin(){
		if( checkDateValidation() )
			return begin.getValue();
		else
			return null;
	}
	
	public Date getEnd(){
		return end.getValue();
	}
}
